package com.itic.system.collage.skuProperty.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.system.collage.category.persistence.model.CollageCategory;
import com.itic.system.collage.category.service.CollageCategoryService;
import com.itic.system.collage.sku.persistence.model.CollageSku;
import com.itic.system.collage.sku.service.CollageSkuService;
import com.itic.system.vo.CollageSkuVo;
import com.itic.system.vo.OrderSkuPropertyVo;
import com.itic.system.vo.SkuPropertyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.QueryAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.collage.skuProperty.persistence.dao.CollageSkuPropertyDao;
import com.itic.system.collage.skuProperty.persistence.model.CollageSkuProperty;
import com.itic.system.collage.skuProperty.persistence.model.CollageSkuPropertyExample;
import com.itic.system.collage.skuProperty.persistence.model.CollageSkuPropertyExample.Criteria;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class CollageSkuPropertyService
     extends CrudService<CollageSkuPropertyDao,CollageSkuProperty,CollageSkuPropertyExample> {
    @Autowired
    private CollageSkuPropertyDao collageSkuPropertyDao;
    @Autowired
    private CollageSkuService collageSkuService;
    @Autowired
    private CollageCategoryService collageCategoryService;

    /**
     * 根据拼团Id查询拼团属性值匹配集合
     * @param collageId
     * @return
     */
    public List<CollageSkuProperty> findCollageSkuPropertyByCollageId(String collageId){
        CollageSkuPropertyExample example = new CollageSkuPropertyExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andCollageIdEqualTo(collageId);
        List<CollageSkuProperty> collageSkuProperties = this.list(example);
        return collageSkuProperties;
    }

    /**
     * 批量修改拼团属性值匹配的库存
     * @param collageSkuVos
     */
    public void editBatchCollageSkuPropertyInventorysByCollageSkuVo(List<CollageSkuVo> collageSkuVos){
        for(CollageSkuVo collageSkuVo : collageSkuVos){
            //根据id查询
            CollageSkuProperty collageSkuProperty = this.get(collageSkuVo.getPropertyId());
            int inventorys = collageSkuVo.getInventorys() ==null ?1:collageSkuVo.getInventorys().intValue();
            collageSkuProperty.setInventorys(inventorys);
            this.edit(collageSkuProperty);
        }
    }

    /**
     * 根据拼团id以及属性匹配查询拼团属性匹配
     * @param colalgeId 拼团Id
     * @param property 属性值匹配 a:b:c
     * @return
     */
    public CollageSkuProperty getCollageSkuPropertyByCollageIdAndProperty(String colalgeId,String property){
        Map<String, Object> params = new HashMap<>();
        params.put("collageId",colalgeId);
        params.put("property",property);
        CollageSkuProperty collageSkuProperty = this.collageSkuPropertyDao.getCollageSkuPropertyByCollageIdAndProperty(params);
        return collageSkuProperty;
    }

    /**
     * CollageSkuProperty 转换成 OrderSkuPropertyVo
     * @param collageSkuProperty
     * @return
     */
    public List<OrderSkuPropertyVo>  collageSkuPropertyWrapToOrderSkuPropertyVo(CollageSkuProperty collageSkuProperty){
        List<OrderSkuPropertyVo> orderSkuPropertyVos = new ArrayList<>();
        String propertySkuIds = collageSkuProperty.getProperty();
        String skuIds [] = propertySkuIds.split(":");
        for(String skuId :skuIds){
            OrderSkuPropertyVo orderSkuPropertyVo = new OrderSkuPropertyVo();
            CollageSku collageSku = this.collageSkuService.get(skuId);
            orderSkuPropertyVo.setSkuValue(collageSku.getSkuName());
            CollageCategory collageCategory = this.collageCategoryService.get(collageSku.getCollageCategoryId());
            orderSkuPropertyVo.setSkuName(collageCategory.getCategoryName());
            orderSkuPropertyVos.add(orderSkuPropertyVo);
        }
        return orderSkuPropertyVos;
    }

    /**
     * 效验拼团是否存在属性匹配数据
     * @param collageId
     * @return true 存在 else  不存在
     */
    public boolean isExistCollageSkuPropertyByCollageId(String collageId){
        List<CollageSkuProperty> collageSkuProperties = this.findCollageSkuPropertyByCollageId(collageId);
        if(!collageSkuProperties.isEmpty())
            return true;
        else
            return false;
    }

    /**
     * 匹配商品属性(目前只支持两个属性匹配)
     * @param collageCategories
     * @return
     */
    public Map<String,Object> matchSkuProperty(List<CollageCategory>  collageCategories){
        Map<String,Object> skuPropertyMap = new HashMap<>();
        int categoryCount = collageCategories.size();
        switch (categoryCount){
            case 1:
                List<CollageSku> collageSkus = this.collageSkuService.findCollageSkuByCategoryId(collageCategories.get(0).getId());
                for(CollageSku  collageSku :collageSkus)
                    skuPropertyMap.put(collageSku.getId(),collageSku.getSkuName());
                break;
            case 2:
                List<CollageSku> oneCollageSkus = this.collageSkuService.findCollageSkuByCategoryId(collageCategories.get(0).getId());
                List<CollageSku> twoCollageSkus = this.collageSkuService.findCollageSkuByCategoryId(collageCategories.get(1).getId());
                for(CollageSku oneCollageSku : oneCollageSkus){
                    for(CollageSku twoCollageSku: twoCollageSkus){
                        StringBuffer skuProperty = new StringBuffer(oneCollageSku.getId()).append(":").append(twoCollageSku.getId());
                        StringBuffer skuNameProperty = new StringBuffer(oneCollageSku.getSkuName()).append("/").append(twoCollageSku.getSkuName());
                        skuPropertyMap.put(skuProperty.toString(),skuNameProperty.toString());
                    }
                }
                break;
        }
        return skuPropertyMap;
    }

    /**
     * 添加拼团属性property
     * @param collageId
     * @param property
     * @param inventorys
     * @param propertyName
     * @return
     */
    public CollageSkuProperty addCollageSkuProperty(String collageId,String property,Integer inventorys, String propertyName){
        CollageSkuProperty collageSkuProperty = new CollageSkuProperty();
        collageSkuProperty.setCollageId(collageId);
        collageSkuProperty.setProperty(property);
        collageSkuProperty.setPropertyName(propertyName);
        collageSkuProperty.setInventorys(inventorys);
        String result = this.add(collageSkuProperty);
        if(SUCCESS.equals(result))
            return collageSkuProperty;
        else
            return null;
    }

    /**
     * 查看不存在的规格属性property
     * @param collageId 拼团Id
     * @param properties 属性值匹配集合 a:b:c
     * * @return
     */
    public List<CollageSkuProperty> findCollageSkuPropertyByCollageIdAndNotExistProperties(String collageId,List<String> properties){
        CollageSkuPropertyExample example = new CollageSkuPropertyExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andCollageIdEqualTo(collageId);
        criteria.andPropertyNotIn(properties);
        List<CollageSkuProperty> collageSkuProperties = this.list(example);
        return collageSkuProperties;
    }

    /**
     *CollageSkuProperty 转换成 SkuPropertyVo
     * @param collageSkuProperty
     * @return
     */
    public SkuPropertyVo collageSkuPropertyWrapToSkuPropertyVo(CollageSkuProperty collageSkuProperty){
        SkuPropertyVo skuPropertyVo = new SkuPropertyVo();
        skuPropertyVo.setInventory(collageSkuProperty.getInventorys());
        skuPropertyVo.setPropertyId(collageSkuProperty.getId());
        skuPropertyVo.setPropertyName(collageSkuProperty.getPropertyName());
        return skuPropertyVo;
    }
}